package com.cpems.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cpems.common.core.domain.entity.SysDictData;
import com.cpems.common.core.page.TableDataInfo;
import com.cpems.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cpems.common.utils.StringUtils;
import com.cpems.system.mapper.SysDictDataMapper;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.stereotype.Service;
import com.cpems.system.domain.bo.SysProjectInfoBo;
import com.cpems.system.domain.vo.SysProjectInfoVo;
import com.cpems.system.domain.SysProjectInfo;
import com.cpems.system.mapper.SysProjectInfoMapper;
import com.cpems.system.service.ISysProjectInfoService;

import java.text.DecimalFormat;
import java.util.*;

/**
 * 项目信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-19
 */
@RequiredArgsConstructor
@Service
public class SysProjectInfoServiceImpl implements ISysProjectInfoService {

    private final SysProjectInfoMapper baseMapper;
    private final SysDictDataMapper dictDatabaseMapper;
    /**
     * 查询项目信息
     */
    @Override
    public SysProjectInfoVo queryById(Long projectId){
        return baseMapper.selectVoById(projectId);
    }

    /**
     * 查询项目信息列表
     */
    @Override
    public TableDataInfo<SysProjectInfoVo> queryPageList(SysProjectInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysProjectInfo> lqw = buildQueryWrapper(bo);
        Page<SysProjectInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目信息列表
     */
    @Override
    public List<SysProjectInfoVo> queryList(SysProjectInfoBo bo) {
        LambdaQueryWrapper<SysProjectInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysProjectInfo> buildQueryWrapper(SysProjectInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysProjectInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTypeId()), SysProjectInfo::getTypeId, bo.getTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), SysProjectInfo::getProjectName, bo.getProjectName());
        lqw.like(bo.getTargetCop() != null, SysProjectInfo::getTargetCop, bo.getTargetCop());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysProjectInfo::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增项目信息
     */
    @Override
    public Boolean insertByBo(SysProjectInfoBo bo) {
        SysProjectInfo add = BeanUtil.toBean(bo, SysProjectInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProjectId(add.getProjectId());
        }
        return flag;
    }

    /**
     * 修改项目信息
     */
    @Override
    public Boolean updateByBo(SysProjectInfoBo bo) {
        SysProjectInfo update = BeanUtil.toBean(bo, SysProjectInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysProjectInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取全部不同类型的项目数量及占比
     */
    @Override
    public List<SysProjectInfoVo> getAllProjectType(String DictType) {
        List<SysProjectInfoVo> list = new ArrayList<>();
        LambdaQueryWrapper<SysProjectInfo> lqw = new LambdaQueryWrapper<>();
        Long count = baseMapper.selectCount(lqw);
        val sysProjectStatus = dictDatabaseMapper.selectList(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getDictType,DictType));
        for(val sysProject :sysProjectStatus){
            SysProjectInfoVo sysProjectInfoVo = new SysProjectInfoVo();
            Long statusCount;
            if (Objects.equals(DictType, "sys_project_type")){
                statusCount =baseMapper.selectCount(new LambdaQueryWrapper<SysProjectInfo>().eq(SysProjectInfo::getTypeId, sysProject.getDictValue()));
                sysProjectInfoVo.setTypeId(sysProject.getDictValue());
            }else {
                statusCount =baseMapper.selectCount(new LambdaQueryWrapper<SysProjectInfo>().eq(SysProjectInfo::getStatus, sysProject.getDictValue()));
                sysProjectInfoVo.setStatus(sysProject.getDictValue());
            }
            sysProjectInfoVo.setTypeName(sysProject.getDictLabel());
            sysProjectInfoVo.setCount(statusCount);
            DecimalFormat df = new DecimalFormat("0.00%");
            String percent = df.format((statusCount / count.doubleValue()));
            sysProjectInfoVo.setProportion(percent);
            list.add(sysProjectInfoVo);
        }
        return list;
    }


}
